<template>
  <div class="app-container">
    作业列表
    <!--查询表单-->
    <el-card class="operate-container" shadow="never">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="作业名称">

          <el-input v-model="searchObj.taskName" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item label="老师姓名">
          <el-input v-model="searchObj.teacherName" placeholder="xxx"></el-input>
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form>
    </el-card>
    <!-- 工具按钮 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button class="btn-add" @click="batchRemove()" >批量删除</el-button>
    </el-card>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"/>
      <el-table-column
        label="#"
        width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="taskName" label="作业名称" width="120" />
      <el-table-column prop="teacherName" label="教师名称" width="80"/>
      <el-table-column prop="question" label="问题" />
      <el-table-column prop="startTime" label="发布时间" width="160" />
      <el-table-column prop="endTime" label="结束时间" width="160" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="removeById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import taskApi from '@/api/task/task'
export default {
  data() {
    return{
      list: [], // 作业列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [],// 批量删除选中的记录列表
    }
  },
  created(){
    this.fetchData()
  },
  methods:{
    handleSelectionChange(selection){
      this.multipleSelection = selection
    },
    fetchData(){
      taskApi.pageList(this.page,this.limit,this.searchObj)
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
        })
    },
    changePageSize(size){
      this.limit = size;
      this.fetchData()
    },
    changeCurrentPage(page){
      this.page = page;
      this.fetchData()
    },
    resetData(){
      this.searchObj={}
      this.fetchData()
    },
    removeById(id){
      this.$confirm('此操作将删除作业信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return taskApi.removeCourseId(id)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.msg)
      })
    },
    batchRemove(){
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录！')
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 点击确定，远程调用ajax
        // 遍历selection，将id取出放入id列表
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item.id)
        })
        // 调用api
        return taskApi.batchRemove(idList)
      }).then((response) => {
        this.fetchData()
        this.$message.success(response.msg)
      }).catch(error => {
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
  }
}
</script>
